package com.cyl.manager.act.controller;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.enums.BusinessType;
import com.cyl.external.MyEasyExcelUtil;
import com.cyl.manager.act.convert.CouponCategoryConvert;
import com.cyl.manager.act.domain.entity.CouponCategory;
import com.cyl.manager.act.domain.query.MemberCouponTjQuery;
import com.cyl.manager.act.domain.vo.MemberCouponTjVO;
import com.cyl.manager.act.mapper.MemberCouponMapper;
import com.cyl.manager.act.pojo.query.CouponCategoryByDpQuery;
import com.cyl.manager.act.pojo.query.CouponCategoryQuery;
import com.cyl.manager.act.service.CouponCategoryService;
import com.cyl.manager.act.service.MemberCouponService;
import com.cyl.manager.aws.domain.entity.ExportExcel;
import com.cyl.manager.oms.domain.vo.ManagerOrderVO;
import com.cyl.manager.act.pojo.vo.CouponCategoryByDpVO;
import com.cyl.manager.act.pojo.vo.CouponCategoryVO;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
/**
 * 福利券分类Controller
 *
 * @author zcc
 * @date 2025-02-16
 */
@Api(description ="福利券分类接口列表")
@RestController
@RequestMapping("/act/couponCategory")
public class CouponCategoryController extends BaseController {
    @Autowired
    private CouponCategoryService service;
    @Autowired
    private CouponCategoryConvert convert;
    
    @Autowired
    private MemberCouponService memberCouponService;
    
    @Autowired
    private  MemberCouponMapper memberCouponMapper;
    
     @ApiOperation("福利券分类统计按店铺")
	 @GetMapping("/selectMemberCouponGrepDp")
	 @PreAuthorize("@ss.hasPermi('act:couponCategory:syjltj')")
	 public ResponseEntity<List<MemberCouponTjVO>> selectMemberCouponGrepDp(MemberCouponTjQuery mctq) {
		 	Long couponCategoryId = mctq.getCouponCategoryId();
	    	Long deptDpId = mctq.getDeptDpId();
	    	if(couponCategoryId==null && deptDpId == null) {
		        throw new RuntimeException("店铺或福利券二选其一");
	    	}
	        return ResponseEntity.ok(memberCouponMapper.selectMemberCouponGrepDp(mctq));
	 }
	 
     @ApiOperation("福利券分类统计按人员")
	 @GetMapping("/selectMemberCouponGrepMember")
	 @PreAuthorize("@ss.hasPermi('act:couponCategory:syjltj')")
	 public ResponseEntity<List<MemberCouponTjVO>> selectMemberCouponGrepMember(MemberCouponTjQuery mctq) {
	    	Long couponCategoryId = mctq.getCouponCategoryId();
	    	Long deptDpId = mctq.getDeptDpId();
	    	if(deptDpId==null || couponCategoryId == null) {
		        throw new RuntimeException("店铺和福利券不填");
	    	}
		 	mctq.setDeptDpId(deptDpId);
		 	mctq.setCouponCategoryId(couponCategoryId);
	        return ResponseEntity.ok(memberCouponMapper.selectMemberCouponGrepMember(mctq));
	 }
     
     @ApiOperation("福利券分类统计按店铺导出")
   	 @GetMapping("/selectMemberCouponGrepDpExport")
   	 @PreAuthorize("@ss.hasPermi('act:couponCategory:syjltjexport')")
   	 public ResponseEntity<ExportExcel> selectMemberCouponGrepDpExport(MemberCouponTjQuery mctq) {
   		 	Long couponCategoryId = mctq.getCouponCategoryId();
   	    	Long deptDpId = mctq.getDeptDpId();
   	    	if(couponCategoryId==null && deptDpId == null) {
   		        throw new RuntimeException("店铺或福利券二选其一");
   	    	}
   		 	List<MemberCouponTjVO> list = memberCouponMapper.selectMemberCouponGrepDp(mctq);
   		 	if(list==null) {
		        throw new RuntimeException("没有可导出的数据");
		 	}
   		 	long dateSize = list.size();
   	        long thisTime = Long.parseLong("1"+DateUtil.format(DateUtil.date(), "yyyyMMddHHssSSS")+ SecurityUtils.getUserId());
	   		ExportExcel exportExcel = new ExportExcel(JSONUtil.toJsonStr(mctq), "福利券分类统计按店铺导出"+thisTime+".xlsx", "福利券分类统计按店铺导出", thisTime, dateSize);
	   		new MyEasyExcelUtil<MemberCouponTjVO>(MemberCouponTjVO.class, exportExcel).writeListAll(list);
	   		return ResponseEntity.ok(exportExcel);
//	   		
//	   		
//   	        ExcelUtil<MemberCouponTjVO> util = new ExcelUtil<>(MemberCouponTjVO.class);
//   	        return ResponseEntity.ok(util.writeExcel(list, "福利券分类统计按店铺导出"));
   	 }
   	 
     @ApiOperation("福利券分类统计按人员导出")
   	 @GetMapping("/selectMemberCouponGrepMemberExport")
   	 @PreAuthorize("@ss.hasPermi('act:couponCategory:syjltjexport')")
   	 public ResponseEntity<ExportExcel> selectMemberCouponGrepMemberExport(MemberCouponTjQuery mctq) {
   	    	Long couponCategoryId = mctq.getCouponCategoryId();
   	    	Long deptDpId = mctq.getDeptDpId();
   	    	if(deptDpId==null || couponCategoryId == null) {
   		        throw new RuntimeException("店铺和福利券不填");
   	    	}
   		 	mctq.setDeptDpId(deptDpId);
   		 	mctq.setCouponCategoryId(couponCategoryId);
   		 	List<MemberCouponTjVO> list = memberCouponMapper.selectMemberCouponGrepMember(mctq);
   		 	if(list==null) {
   		        throw new RuntimeException("没有可导出的数据");
   		 	}
   		 	long dateSize = list.size();
   	        long thisTime = Long.parseLong("1"+DateUtil.format(DateUtil.date(), "yyyyMMddHHssSSS")+ SecurityUtils.getUserId());
	   		ExportExcel exportExcel = new ExportExcel(JSONUtil.toJsonStr(mctq), "福利券分类统计按人员导出"+thisTime+".xlsx", "福利券分类统计按人员导出", thisTime, dateSize);
	   		new MyEasyExcelUtil<MemberCouponTjVO>(MemberCouponTjVO.class, exportExcel).writeListAll(list);
	   		return ResponseEntity.ok(exportExcel);
//   	        ExcelUtil<MemberCouponTjVO> util = new ExcelUtil<>(MemberCouponTjVO.class);
//   	        return ResponseEntity.ok(util.writeExcel(list, "福利券分类统计按人员导出"));

   	 }
        
	 
	 

    @ApiOperation("查询福利券分类列表")
    @PreAuthorize("@ss.hasPermi('act:couponCategory:list')")
    @PostMapping("/list")
    public ResponseEntity<Page<CouponCategory>> list(@RequestBody CouponCategoryQuery query, Pageable page) {
        Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
        	query.setDeptZbjId(qrDeptZbjId);
        }
        List<CouponCategory> list = service.selectList(query, page);
        return ResponseEntity.ok(new PageImpl<>(list, page, ((com.github.pagehelper.Page)list).getTotal()));
    }

    @ApiOperation("导出福利券分类列表")
    @PreAuthorize("@ss.hasPermi('act:couponCategory:export')")
    @Log(title = "福利券分类", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public ResponseEntity<String> export(CouponCategoryQuery query) {
        Long qrDeptZbjId = SecurityUtils.getDeptZbjId();
        if(qrDeptZbjId!=null) {
        	query.setDeptZbjId(qrDeptZbjId);
        }
        List<CouponCategory> list = service.selectList(query, null);
        ExcelUtil<CouponCategoryVO> util = new ExcelUtil<>(CouponCategoryVO.class);
        return ResponseEntity.ok(util.writeExcel(convert.dos2vos(list), "福利券分类数据"));
    }

    @ApiOperation("获取福利券分类详细信息")
    @PreAuthorize("@ss.hasPermi('act:couponCategory:query')")
    @GetMapping(value = "/{id}")
    public ResponseEntity<CouponCategory> getInfo(@PathVariable("id") Long id) {
        return ResponseEntity.ok(service.selectById(id));
    }

    @ApiOperation("新增福利券分类")
    @PreAuthorize("@ss.hasPermi('act:couponCategory:add')")
    @Log(title = "福利券分类", businessType = BusinessType.INSERT)
    @PostMapping
    public ResponseEntity<Integer> add(@RequestBody CouponCategory couponCategory) {
        return ResponseEntity.ok(service.insert(couponCategory));
    }

    @ApiOperation("修改福利券分类")
    @PreAuthorize("@ss.hasPermi('act:couponCategory:edit')")
    @Log(title = "福利券分类", businessType = BusinessType.UPDATE)
    @PutMapping
    public ResponseEntity<Integer> edit(@RequestBody CouponCategory couponCategory) {
        return ResponseEntity.ok(service.update(couponCategory));
    }

    @ApiOperation("删除福利券分类")
    @PreAuthorize("@ss.hasPermi('act:couponCategory:remove')")
    @Log(title = "福利券分类", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public ResponseEntity<Integer> remove(@PathVariable Long[] ids) {
        return ResponseEntity.ok(service.deleteByIds(ids));
    }
    
    @ApiOperation("导出福利券分类列表")
    @PreAuthorize("@ss.hasPermi('act:couponCategory:export')")
    @Log(title = "导出福利券按店铺", businessType = BusinessType.EXPORT)
    @GetMapping("/exportByDp")
    public ResponseEntity<String> exportByDp(CouponCategoryByDpQuery query) {
    	List<CouponCategoryByDpVO>  list = memberCouponService.exportByDp(query);
        ExcelUtil<CouponCategoryByDpVO> util = new ExcelUtil<>(CouponCategoryByDpVO.class);
        return ResponseEntity.ok(util.writeExcel(list, "按店铺提交福利券已核销数量"));
    }
    
}
